﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.Collections.Generic;

namespace ALiteTeam.PackageTracer.Core
{
    public static class Settings
    {
        private const string CONFIGURATION = "Configuration";
        private const string APPLICATION_CONTEXT = "AppContext";

        public static readonly DateTime CURRRENT_VERSION = new DateTime(2012, 3, 3); 

        static Settings()
        {
        }

        private static T GetValue<T>(string key)
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains(key) == true)
            {
                return (T)IsolatedStorageSettings.ApplicationSettings[key];
            }
            else
            {
                return default(T);
            }
        }

        private static void Save(string key, object value)
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains(key) == true)
            {
                IsolatedStorageSettings.ApplicationSettings[key] = value;
            }
            else
            {
                IsolatedStorageSettings.ApplicationSettings.Add(key, value);
            }

            IsolatedStorageSettings.ApplicationSettings.Save();
        }

        public static AppContext AppContext
        {
            get 
            {
                var appContext = GetValue<AppContext>(APPLICATION_CONTEXT);
                if (appContext == null)
                {
                    appContext = new AppContext();
                    Save(APPLICATION_CONTEXT, appContext);
                }

                return appContext;
            }
            set { Save(APPLICATION_CONTEXT, value); }
        }

        public static Configuration Configuration
        {
            get 
            { 
                var config =  GetValue<Configuration>(CONFIGURATION);
                if (config == null)
                {
                    config = new Configuration();
                    Save(CONFIGURATION, config);
                }

                return config;
            }
            set { Save(CONFIGURATION, value); }
        }
    }
}
